package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;

import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 删除排班接口
     *
     * @param request
     * @return
     */
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        // 调用公共方法 获取传递的医院信息 将返回的map集合调用工具类值改为object
        Map<String, Object> paramMap = getStringObjectMap(request);
        // 调用公共方法获取医院编号 查询并判断签名 获取排班编号
        String hoscode = getHoscode(paramMap);
        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        //调用service方法
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    /**
     * 查询排班接口
     *
     * @param request
     * @return
     */
    @PostMapping("schedule/list")
    public Result scheduleList(HttpServletRequest request) {
        // 调用公共方法 获取传递的医院信息 将返回的map集合调用工具类值改为object
        Map<String, Object> paramMap = getStringObjectMap(request);
        // 调用公共方法获取医院编号 查询并判断签名 获取科室编号
        String hoscode = getHoscode(paramMap);
        String depcode = (String) paramMap.get("depcode");

        // 获取当前页和每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        // 把医院编号作为条件封装到DepartmentQueryVo对象中
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        // 调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 上传排班接口
     *
     * @param request
     * @return
     */
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        // 调用公共方法 获取传递的医院信息 将返回的map集合调用工具类值改为object
        Map<String, Object> paramMap = getStringObjectMap(request);
        // 调用公共方法获取医院编号 查询并判断签名
        getHoscode(paramMap);

        // 调用service方法
        scheduleService.save(paramMap);
        return Result.ok();
    }

    /**
     * 删除科室接口
     *
     * @param request
     * @return
     */
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        // 调用公共方法 获取传递的医院信息 将返回的map集合调用工具类值改为object
        Map<String, Object> paramMap = getStringObjectMap(request);

        // 调用公共方法获取医院编号 查询并判断签名 获取科室编号
        String hoscode = getHoscode(paramMap);
        String depcode = (String) paramMap.get("depcode");

        //调用service方法
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    /**
     * 查询科室接口
     *
     * @param request
     * @return
     */
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        // 调用公共方法 获取传递的医院信息 将返回的map集合调用工具类值改为object
        Map<String, Object> paramMap = getStringObjectMap(request);

        // 获取当前页和每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        // 调用公共方法获取医院编号 查询并判断签名
        String hoscode = getHoscode(paramMap);

        // 把医院编号作为条件封装到DepartmentQueryVo对象中
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        // 调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 上传科室接口
     *
     * @param request
     * @return
     */
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        // 调用公共方法 获取传递的医院信息 将返回的map集合调用工具类值改为object
        Map<String, Object> paramMap = getStringObjectMap(request);

        // 调用公共方法获取医院编号 查询并判断签名
        getHoscode(paramMap);

        // 调用service方法
        departmentService.save(paramMap);
        return Result.ok();
    }


    /**
     * 查询医院接口
     *
     * @param request
     * @return
     */
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        // 调用公共方法 获取传递的医院信息 将返回的map集合调用工具类值改为object
        Map<String, Object> paramMap = getStringObjectMap(request);

        // 调用公共方法获取医院编号 查询并判断签名
        String hoscode = getHoscode(paramMap);

        // 调用service方法根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    /**
     * 上传医院接口
     *
     * @param request
     * @return
     */
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) { // 获取hospital-manage post提交的数据
        // 调用公共方法 获取传递的医院信息 将返回的map集合调用工具类值改为object
        Map<String, Object> paramMap = getStringObjectMap(request);

        // 调用公共方法获取医院编号 查询并判断签名
        getHoscode(paramMap);

        // 传输过程中"+" 转换为 " " 要转换回"+"
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        // 调用service方法添加至数据库
        hospitalService.save(paramMap);
        return Result.ok();
    }

    /**
     * 获取传递的医院信息
     *
     * @param request
     * @return
     */
    private Map<String, Object> getStringObjectMap(HttpServletRequest request) {
        // 获取传递的医院信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();
        // 将返回的map集合调用工具类值改为object
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestParameterMap);
        return paramMap;
    }

    /**
     * 获取医院编号 查询并判断签名
     *
     * @param paramMap
     * @return
     */
    private String getHoscode(Map<String, Object> paramMap) {
        // 获取传递的医院编号
        String hoscode = (String) paramMap.get("hoscode");

        // 1.获取医院系统传递的签名 签名进行了md5加密 转为string类型比较
        String hospSign = (String) paramMap.get("sign");

        // 2.根据传递的医院编码 查询签名
        String signKey = hospitalSetService.getSingKey(hoscode);

        // 3.把数据库查询的签名进行md5加密
        String singKeyMd5 = MD5.encrypt(signKey);

        // 4.判断签名是否和传入的签名一致
        if (!hospSign.equals(singKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        return hoscode;
    }

}
